{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 1\n",
    "\n",
    "Create a single data frame containing rides from both January 2019 and January 2020, with a column `year` indicating which year it came from. Use `groupby` to compare the average cost of a taxi in January of each of these two years."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "year\n",
       "2019    15.682222\n",
       "2020    18.663149\n",
       "Name: total_amount, dtype: float64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "jan_2019_filename = '../data/nyc_taxi_2019-01.csv'\n",
    "\n",
    "jan_2019_df = pd.read_csv(jan_2019_filename,\n",
    "                usecols=['passenger_count',\n",
    "                         'trip_distance', 'total_amount'])\n",
    "jan_2019_df['year'] = 2019\n",
    "\n",
    "jan_2020_filename = '../data/nyc_taxi_2020-01.csv'\n",
    "\n",
    "jan_2020_df = pd.read_csv(jan_2020_filename,\n",
    "                usecols=['passenger_count',\n",
    "                         'trip_distance', 'total_amount'])\n",
    "jan_2020_df['year'] = 2020\n",
    "\n",
    "df = pd.concat([jan_2019_df, jan_2020_df])\n",
    "\n",
    "\n",
    "df.groupby('year')['total_amount'].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 2\n",
    "\n",
    "Now create a two-level grouping, first by year and then by `passenger_count`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "year  passenger_count\n",
       "2019  0.0                18.663658\n",
       "      1.0                15.609601\n",
       "      2.0                15.831294\n",
       "      3.0                15.604015\n",
       "      4.0                15.650307\n",
       "      5.0                15.546940\n",
       "      6.0                15.437892\n",
       "      7.0                48.278421\n",
       "      8.0                64.105517\n",
       "      9.0                31.094444\n",
       "2020  0.0                18.059724\n",
       "      1.0                18.343110\n",
       "      2.0                19.050504\n",
       "      3.0                18.736862\n",
       "      4.0                19.128092\n",
       "      5.0                18.234443\n",
       "      6.0                18.367962\n",
       "      7.0                71.143103\n",
       "      8.0                58.197059\n",
       "      9.0                81.244211\n",
       "Name: total_amount, dtype: float64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Group by year, then by passenger_count\n",
    "# The result is a series with a multi-index\n",
    "df.groupby(['year', 'passenger_count'])['total_amount'].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 3\n",
    "\n",
    "Finally, the `corr` method allows us to see how strongly two columns correlate with one another. Use `corr` and then `sort_values` to find which have the highest correlation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>passenger_count</th>\n",
       "      <th>trip_distance</th>\n",
       "      <th>total_amount</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <td>-0.021602</td>\n",
       "      <td>0.001140</td>\n",
       "      <td>0.007657</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>total_amount</th>\n",
       "      <td>-0.000136</td>\n",
       "      <td>0.004331</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.007657</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trip_distance</th>\n",
       "      <td>0.008974</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.004331</td>\n",
       "      <td>0.001140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>passenger_count</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.008974</td>\n",
       "      <td>-0.000136</td>\n",
       "      <td>-0.021602</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 passenger_count  trip_distance  total_amount      year\n",
       "year                   -0.021602       0.001140      0.007657  1.000000\n",
       "total_amount           -0.000136       0.004331      1.000000  0.007657\n",
       "trip_distance           0.008974       1.000000      0.004331  0.001140\n",
       "passenger_count         1.000000       0.008974     -0.000136 -0.021602"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df.corr() returns a square, so we can look at just the first column\n",
    "# and get everything we need\n",
    "\n",
    "# When we sort, we see very clearly that there's basically no correlation\n",
    "# between passenger_count and any other column. (Except for itself, of \n",
    "# course...)  So there really doesn't seem to be any financial advantage\n",
    "# for a driver to take a larger group.\n",
    "df.corr().sort_values('passenger_count')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
